<?php

namespace app\services;

use app\components\XSecurity;
use app\models\User;
use app\extensions\ApiError;
/**
 * UserService业务逻辑类
 * @author chenym
 * @since 2017-07-10
 */
class UserService
{
    public function __construct()
    {
    }

    /**
     * 检查用户名是否能使用
     * @param $groupName
     * @param int $groupId
     * @return bool
     */
    public function canUserNameUsed($userName)
    {
        $fields           = "userId";
        $params["where"]  = "userName = :userName";
        $params["bind"] = [":userName" => $userName];
        
        $user = new User();
        $res  = $user->dbGet($fields, 0, $params);
        if (!$res) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 注册用户
     * @param $userName
     * @param $pwd
     * @return bool
     */
    public function register($userName, $pwd,$rePwd, $channel = '系统')
    {
        if(empty($userName) || empty($pwd)){
            throw new ApiError("用户名或密码不能为空",0);
        }
        if(strlen($pwd) < 6 || strlen($pwd) > 24){
            throw new ApiError("密码长度不符",0);
        }
        //校验密码
        if($pwd !== $rePwd){
            throw new ApiError("两次密码不一致",0);
        }

        $res      = $this->canUserNameUsed($userName);
        if(!$res){
            throw new ApiError("用户已存在",0);
        }

        $pwd  = $this->hashPassword($userName, $pwd);
        $data = ['userName' => $userName, 'password' => $pwd, 'channel' => $channel];

        $user = new User();
        $res  = $user->dbAdd($data);

        if ($res) {
            $data["status"] = 1;
            $data["data"]  = $this->getUserToken($res);
        } else {
            $data["errCode"] = 0;
            $data["errMsg"]  = "注册失败";
        }
        return $data;
    }
    /**
     * 密码进行加密
     * @param $username
     * @param $password 密码明文
     * @return string
     */
    public function hashPassword($userName, $password)
    {
        return md5(md5($userName) . md5($password) . "@dq123");
    }
    /**
     * 登录
     * @param $username 用户名称
     * @param $password 密码明文
     * @return string
     */
    public function login($userName, $password)
    {
        $user = new User();
        $fields = 'userId,userName,password,phone';
        $param["where"] = "userName = :userName";
        $param["bind"] = [":userName" => $userName];
        $info = $user->dbGet($fields,0,$param);

        if(!$info || ($this->hashPassword($userName,$password) != $info["password"])){
            throw new ApiError("用户或者密码不对", 0);
        }
        unset($info["password"]);
        $data["status"] = true;
        $data["token"]  = $this->getUserToken($info["userId"],$info);
        return $data;

    }
    /**
     * 获取用户的token
     * @param $userId 用户ID
     * @return string
     */
    public function getUserToken($userId,$uinfo = [])
    {
        if(empty($uinfo)){
            $fields = 'userId,userName,phone';
            $user = new User();
            $uinfo  = $user->dbGet($fields, $userId);
        }
        
        $uinfo  = $uinfo["userId"] . "##" . $uinfo["userName"] . "##" . $uinfo["phone"] . "##" . json_encode($uinfo);

        return XSecurity::apiSTD3Encrypt(rawurlencode($uinfo));
    }

}
